<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        setTimeout(function () {
          console.log(1)
        })
        new Promise(function (resolve, reject) {
          console.log(2)
          resolve(3)
        }).then(function (val) {
          console.log(val)
        })
        console.log(4)
        // 2 4 3 1


        // 事件循环（eventloop）:
        // 1、主线程上面的同步代码，
        // 2、取出微任务列表里面所有的微任务，全部执行
        // 3、取出宏任务队列里面的一个宏任务，执行
        // 4、继续执行第2步
        // 5、继续执行第3步
        // 6、继续执行第2步
        // 7、继续执行第3步





        console.log(1)

        setTimeout(function () {
        console.log(2)
        new Promise(function (resolve) {
            console.log(3)
            resolve()
        }).then(function () {
            console.log(4)
        })
        })

        const p = new Promise((resolve, reject) => {
        console.log(5)
        resolve() // 标记为成功
        console.log(6)
        })

        p.then((data) => {
        console.log(7)
        })

        console.log(8)
        //1 5 6 8 7 2 3 4










            setTimeout(() => {
                console.log(1)
            }, 0)
            new Promise((resolve,reject)=>{
                console.log(2)
                resolve('p1')
                new Promise((resolve,reject)=>{
                    console.log(3)
                    setTimeout(() => {
                        resolve('setTimeout2')
                        console.log(4)
                    }, 0)
                    resolve('p2')
                }).then(data=>{
                    console.log(data)
                })
                setTimeout(()=>{
                    resolve('setTimeout1')
                    console.log(5);
                },0)
            }).then(data=>{
                console.log(data)
            })
            console.log(6)


      </script>
</body>
</html>